/*
    This file is part of FSJController.

    FSJController is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    FSJController is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FSJController.  If not, see <http://www.gnu.org/licenses/>.
*/

package de.qsc.fsjcontroller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Abstract class that represents a building block in a callflow
 * 
 * @author she
 */
abstract public class FSBuildingBlock {

    static Logger logger = LoggerFactory.getLogger(FSBuildingBlock.class);

    // the channel that follows the call flow
    protected FSChannel channel;

    // the block that hooks in after this block is finished
    private FSBuildingBlock nextBuildingBlock;

    public FSBuildingBlock(FSChannel channel) {
        this.channel = channel;
        nextBuildingBlock = this;
    }

    public FSBuildingBlock onEvent(FSEvent event) throws Exception {
        handleEvent(event);
        return nextBuildingBlock;
    }

    protected void runApplication(String appName, String args) {
        channel.runApplication(appName, args);
    }

    public void activateNextBuildingBlock(FSBuildingBlock nextBuildingBlock) {
        this.nextBuildingBlock = nextBuildingBlock;
        this.nextBuildingBlock.start();
    }

    /**
     * initial action when this block gets started by another building block
     */
    abstract public void start();

    /**
     * handles an event passed by the underlying FSChannel. This method should call activateNextBuildingBlock()
     * to advance to the next building block
     * 
     * @param event the FSEvent passed from the controller
     * @throws Exception
     */
    abstract public void handleEvent(FSEvent event) throws Exception;

    abstract public void onFailure();

}
